import store from "@/store"
import { handleGenRequestData } from "@/request"

export default {
  // 上传图片
  uploadImage: (filePath, params = {}) => {
    return new Promise((resolve, reject) => {
      uni.uploadFile({
        url: store.getters.api + "/up/upImg",
        filePath: filePath,
        name: "image",
        formData: handleGenRequestData(params),
        success: (uploadFileRes) => {
          if (uploadFileRes.statusCode === 200) {
            const data = JSON.parse(uploadFileRes.data)
            if (data?.status === "00000" && data?.data?.url) {
              resolve({ url: data.data.url })
            } else {
              uni.$toast(data?.content || "上传失败")
              reject(data?.content || "上传失败")
            }
          } else {
            uni.$toast(uploadFileRes.errMsg)
          }
        }
      })
    })
  }
}
